//
//  AStatusSpeedStrategy.m
//  NetMobileLocation
//
//  Created by Mehmet Sinan Şahin on 2/13/12.
//  Copyright (c) 2012 netgis. All rights reserved.
//

#import "AStatusSpeedStrategy.h"
#import "RotatingNGLocation.h"
#import "Status.h"
#import "NGUtility.h"
#import "NGLocation.h"

@implementation AStatusSpeedStrategy

- (id) initWithLocations:(RotatingNGLocation *)locations {
    if (self = [super init]) {
    }
    return self;
}

- (id) init {
    if (self = [super init]) {
    }
    return self;
}

- (Status) determineStatus:(NGLocation *)currentLocation rLocations:(RotatingNGLocation *)rLocations {
    NSMutableArray * locations = [rLocations toList];
    //[Collections sort:locations];
    if ([rLocations getLocationSendStatus] == NO_ACTION || [rLocations getLocationSendStatus] == _FALSE) {
        LOC_SEND_STATUS atLeastOneLocSent = _FALSE;
        
        for (int i = 0; i < [locations count]; i++) {
            /*
            if ([[locations objectAtIndex:i] sent]) {
                atLeastOneLocSent = _TRUE;
                break;
            }
            */
        }
        
        [rLocations setLocationSendStatus:atLeastOneLocSent];
    }
    if ([locations count] == 0)
        return DURDU;//UYGULAMA_ACILDI;
    if ([[currentLocation speed] floatValue] < eV)
        return DURDU;
    else {
        return HAREKET_EDIYOR;
    }
}


/**
 * şimdilik hız hesaplama bir önceki konuma göre yapılacak Last2Location gibi yapılıyor
 */
- (float) calculateSpeed:(NGLocation *)currentLocation rLocations:(RotatingNGLocation *)rLocations {
    NSMutableArray * locations = [rLocations toList];
    NGLocation * previousLocation = [self getPreviousLocation:currentLocation locations:locations];
    return [NGUtility speed:previousLocation to:currentLocation];
}

- (NGLocation *) getPreviousLocation:(NGLocation *)currentLocation locations:(NSMutableArray *)locations {
    if (![NGUtility isNotEmpty:locations])
        return nil;
    NSArray * locs = [NGUtility sort:locations comparator:^(id a, id b) {
        NSDate *first = [(NGLocation *)a tarihSaat];
        NSDate *second = [(NGLocation *)b tarihSaat];
        return [first compare:second];
    }];
    
    for (int i = [locs count] - 1; i >= 0; i--) {
        if ([[locs objectAtIndex:i] isEqualTo:currentLocation])
            continue;
        return [locs objectAtIndex:i];
    }
    
    return nil;
}


/**
 * Locations' speed property must be set before.
 * @param currentLocation
 * @param locations
 * @return
 */
- (BOOL) stopped:(NGLocation *)currentLocation locations:(NSMutableArray *)locations {
    BOOL durdu = YES;
    int n = [locations count];
    if (n > 3) {
        
        for (int i = 0; i < n; i++) {
            if ([((NGLocation *)[locations objectAtIndex:i]).speed floatValue] > eV) {
                durdu = NO;
                break;
            }
        }
        
        float vAvarage = [NGUtility speed:[locations objectAtIndex:0] to:[locations objectAtIndex:n - 1]];
        if (vAvarage > eV) {
            durdu = NO;
        }
    }
    else {
        durdu = [currentLocation.speed floatValue] < eV;
    }
    return durdu;
}

@end
